#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , personInfoPage(nullptr)  // 初始化成员变量为 nullptr
{
    ui->setupUi(this);

    // 连接按钮的点击事件到槽函数
    connect(ui->pushButton_gotoPersonInfoPage, &QPushButton::clicked,
            this, &MainWindow::on_pushButton_gotoPersonInfoPage_clicked);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete personInfoPage;  // 清理 personInfoPage
}

void MainWindow::on_pushButton_gotoPersonInfoPage_clicked()
{
    // 检查 personInfoPage 是否已经存在
    if (!personInfoPage) {
        // 创建并显示 PersonInfoPage
        personInfoPage = new PersonInfoPage(this);
        personInfoPage->setAttribute(Qt::WA_DeleteOnClose); // 窗口关闭时自动删除
        connect(personInfoPage, &QWidget::destroyed, [this]() {
            personInfoPage = nullptr; // 窗口关闭后将指针设为空
            this->show(); // 返回并显示主窗口
        });
    }

    personInfoPage->show();
    this->hide(); // 隐藏 MainWindow
}
